package com.secondhand.project.controller;

import com.secondhand.common.annotation.Log;
import com.secondhand.common.core.controller.BaseController;
import com.secondhand.common.core.domain.AjaxResult;
import com.secondhand.common.core.page.TableDataInfo;
import com.secondhand.common.enums.BusinessType;
import com.secondhand.common.utils.poi.ExcelUtil;
import com.secondhand.project.domain.ChatGroup;
import com.secondhand.project.service.IChatGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/project/group")
public class ChatGroupController extends BaseController {
    @Autowired
    private IChatGroupService chatGroupService;

    /**
     * 聊天列表
     */
    @PreAuthorize("@ss.hasPermi('project:group:list')")
    @GetMapping("/selectUserGroup")
    public AjaxResult selectUserGroup() {
        List<ChatGroup> chatGroupList = chatGroupService.selectUserGroup();
        return success(chatGroupList);
    }

    @PreAuthorize("@ss.hasPermi('project:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatGroup chatGroup) {
        startPage();
        List<ChatGroup> list = chatGroupService.selectChatGroupList(chatGroup);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:group:export')")
    @Log(title = "聊天组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChatGroup chatGroup) {
        List<ChatGroup> list = chatGroupService.selectChatGroupList(chatGroup);
        ExcelUtil<ChatGroup> util = new ExcelUtil<ChatGroup>(ChatGroup.class);
        util.exportExcel(response, list, "聊天组数据");
    }

    @PreAuthorize("@ss.hasPermi('project:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(chatGroupService.selectChatGroupById(id));
    }

    @PreAuthorize("@ss.hasPermi('project:group:add')")
    @Log(title = "聊天组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatGroup chatGroup) {
        return toAjax(chatGroupService.insertChatGroup(chatGroup));
    }

    @PreAuthorize("@ss.hasPermi('project:group:edit')")
    @Log(title = "聊天组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatGroup chatGroup) {
        return toAjax(chatGroupService.updateChatGroup(chatGroup));
    }

    @PreAuthorize("@ss.hasPermi('project:group:remove')")
    @Log(title = "聊天组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(chatGroupService.deleteChatGroupByIds(ids));
    }
}
